package com.qzsoft.zhjd.machine.modules.machineinfo.dto;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;

import org.springframework.format.annotation.DateTimeFormat;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.qzsoft.jeemis.common.annotation.Dict;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;

/**
 * @ClassName MachineDepreciationDto
 * @Description 设备折旧
 * @author linhuawei
 * @since 2021-11-17
 */
@Data
@EqualsAndHashCode(callSuper = false)
@NoArgsConstructor
@AllArgsConstructor
@Builder
@ToString
public class MachineDepreciationDto {

    /**
     * 设备主键
     */
    @TableId(type = IdType.ASSIGN_UUID)
    private String id;

    /**
     * 单位编码
     */
    private String deptId;

    /**
     * 单位主键
     */
    private String deptPkid;
    
    /**
     * 单位名称
     */
    @TableField(exist = false)
    @Dict(dicCodeField = "deptId", dictCodeId = "UN")
    private String deptName;

    /**
     * 设备名称
     */
    private String name;

    /**
     * 设备编码
     */
    private String machineKey;

    /**
     * 设备类型
     */
    private String type;

    /**
     * 设备类型
     */
    @Dict(dicCodeField = "type", dictCodeId = "MACHINE_TYPE")
    @TableField(exist = false)
    private String typeName;

    /**
     * 设备一级分类
     */
    private String category1;

    /**
     * 设备一级分类名称
     */
    @Dict(dicCodeField = "category1", dictCodeId = "MACHINE_CATEGORY")
    @TableField(exist = false)
    private String category1Name;

    /**
     * 设备二级分类
     */
    private String category2;

    /**
     * 设备二级分类名称
     */
    @Dict(dicCodeField = "category2", dictCodeId = "MACHINE_CATEGORY")
    @TableField(exist = false)
    private String category2Name;

    /**
     * 设备品牌
     */
    private String brand;

    /**
     * 设备品牌名称
     */
    @Dict(dicCodeField = "brand", dictCodeId = "MACHINE_BRAND_MODEL")
    @TableField(exist = false)
    private String brandName;

    /**
     * 设备型号
     */
    private String model;

    /**
     * 设备型号名称
     */
    @Dict(dicCodeField = "model", dictCodeId = "MACHINE_BRAND_MODEL")
    @TableField(exist = false)
    private String modelName;

    /**
     * 资产原值
     */
    private BigDecimal originalValue;

    /**
     * 每月折旧金额
     */
    private BigDecimal deprMonthValue;

    /**
     * 开始折旧日期
     */
    @JsonFormat(pattern = "yyyy-MM", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM")
    private Date deprStartDate;

    /**
     * 最大折旧月份
     */
    private Integer deprMaxMonth;

    /**
     * 设备头像
     */
    private String cover;

    /**
     * 累计折旧月份
     */
    private Integer deprTotalMonth;

    /**
     * 累计折旧金额
     */
    private BigDecimal deprTotalValue;

    /**
     * 资产净值
     */
    private BigDecimal netValue;


    public void count() {
        /* 计算折旧相关 */
        // 累计折旧月份
        if (null != deprStartDate) {
            // 获取当前年份、月份
            Calendar cal = Calendar.getInstance();
            int endMonth = cal.get(Calendar.MONTH) + 1;
            int endYear = cal.get(Calendar.YEAR);
            

            // 获取开始折旧年份、月份
            cal.setTime(deprStartDate);
            int startMonth = cal.get(Calendar.MONTH) + 1;
            int startYear = cal.get(Calendar.YEAR);
            
            
            // 累计折旧月份（不包含当前月份）
            deprTotalMonth = (endYear * 12 + endMonth) - (startYear * 12 + startMonth);
            if(deprTotalMonth > deprMaxMonth) {
                deprTotalMonth = deprMaxMonth;
            }
            
            // 累计折旧金额
            deprTotalValue = deprMonthValue.multiply(new BigDecimal(deprTotalMonth));
            
            // 净值
            netValue = originalValue.subtract(deprTotalValue);

        }
    }

}
